package edu.sctu.jsoup.service;

import edu.sctu.jsoup.entity.ArticleEntity;
import edu.sctu.jsoup.repo.ArticleRepo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class ArticleService {

    @Autowired
    private ArticleRepo articleRepo;

    public void writeBreakPoint() throws IOException {

        //创建断点ID记录
        File writename = new File("D:\\dev\\writeBreakPoint.txt");

        writename.createNewFile(); // 创建新文件

        BufferedWriter out = new BufferedWriter(new FileWriter(writename));

        out.write("测试写入文件\r\n"); // \r\n即为换行
        out.flush();
        out.close();
    }


    public void run() throws IOException {

        //读取TXT文件的每一行数据
        String pathName = "D:\\dev\\poi_ids.txt";

        // 读取txt文件
        File filename = new File(pathName);

        // 建立一个输入流对象reader
        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename));


        BufferedReader br = new BufferedReader(reader);

        String line = "";
        line = br.readLine();

        ArticleEntity entity = new ArticleEntity();
        entity.setId((long) 1);
        for (entity.getId(); line != null; ) {
            entity = new ArticleEntity();
            entity.setData(line);

            entity.setHtml_url("http://www.mafengwo.cn/poi/" + line + ".html");

            Document document = Jsoup.connect(entity.getHtml_url()).get();

            entity.setHtml_content(String.valueOf(document).getBytes("utf-8"));

            entity.setCreate_time(entity.getCreate_time());

            articleRepo.save(entity);

            // 一次读入一行数据
            line = br.readLine();

            if (entity.getId() == 5) break;//测试5条数据

        }


    }
}
